package com.example.pufang.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.common.CommonResult;
import com.example.common.PageParamRequest;
import com.example.constant.Constants;
import com.example.pufang.pojo.Admin;
import com.example.pufang.service.AdminService;
import com.example.pufang.vo.PageListVo;
import com.example.utils.DESUtils;
import com.example.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Api(tags = "web管理员接口")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private TokenUtil tokenUtil;


    @Operation(summary = "登录")
    @PostMapping("/login")
    public CommonResult<Map<String, Object>> login(@RequestBody Admin admin) {

        // 判断是否封禁
        if (adminService.getAdminByName(admin.getUsername()).getIsBan()) {
            return CommonResult.failed("当前账号已被封禁，请联系管理员解决");
        }

        Map<String, Object> loginResult = adminService.login(admin);

        if (ObjectUtils.isNotNull(loginResult)) {
            return CommonResult.success(loginResult);
        } else {
            return CommonResult.failed("账号或密码错误");
        }
    }

    @Operation(summary = "获取分页用户列表")
    @GetMapping("/getListByPage")
    public CommonResult<PageListVo> getListByPage(PageParamRequest pageParamRequest, String searchName) {
        return CommonResult.success(adminService.getListByPage(pageParamRequest, searchName));
    }

    @Operation(summary = "获取当前用户资料")
    @GetMapping("/info")
    public CommonResult<Map<String, Object>> getInfo(HttpServletRequest request) {

        HashMap<String, Object> map = new HashMap<>();

        Integer adminId = tokenUtil.getAdminId(request);
        Admin admin = adminService.getById(adminId);

        if (admin.getRoles() == Constants.ADMIN_DEVELOPER) {
            map.put("roles", Arrays.asList("developer"));
        } else if (admin.getRoles() == Constants.ADMIN_SUPER_ADMIN) {
            map.put("roles", Arrays.asList("super-admin"));
        } else if (admin.getRoles() == Constants.ADMIN_ADMIN) {
            map.put("roles", Arrays.asList("normal-admin"));
        } else {
            map.put("roles", Arrays.asList("unknown"));
        }

        map.put("username", admin.getUsername());
        map.put("avatar", admin.getAvatar());

        return CommonResult.success(map);
    }

    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public CommonResult<Boolean> logout(HttpServletRequest request) {

        Boolean result = tokenUtil.deleteAdminToken(request);

        return CommonResult.success(result);
    }

    @Operation(summary = "封禁账号")
    @PostMapping("/ban")
    public CommonResult<Boolean> ban(@RequestBody Admin admin) {
        Boolean result = adminService.ban(admin);
        if (result) {
            return CommonResult.success(true, "封禁成功");
        } else {
            return CommonResult.failed("封禁失败");
        }
    }

    @Operation(summary = "解封账号")
    @PostMapping("cancelBan")
    public CommonResult<Boolean> cancelBan(@RequestBody Admin admin) {
        Boolean result = adminService.cancelBan(admin);
        if (result) {
            return CommonResult.success(true, "解封成功");
        } else {
            return CommonResult.failed("解封失败");
        }
    }

    @Operation(summary = "添加管理员账号")
    @PostMapping("add")
    public CommonResult<Boolean> add(@RequestBody Admin admin) {

        if (ObjectUtils.isNotNull(adminService.getAdminByName(admin.getUsername()))) {
            return CommonResult.success(false, "当前账户已存在，尝试更换账户注册");
        }

        Boolean result = adminService.add(admin);
        if (result) {
            return CommonResult.success(true, "添加成功");
        } else {
            return CommonResult.success(false, "添加失败");
        }
    }

}
